<?php
/**
 * RedCross Application
 *
 */

/**
 * Controller for calendar functions
 *
 * @category    RedCross
 * @package     Admin_Controller
 */
class Admin_CalendarController extends Zend_Controller_Action
{

	const PARAMETER_DATE_FORMAT = 'DDMMYY';
    private $_calendarService;    
    private $_memberService;
    private $_passiveService;
    private $_shiftService;
	
    public function init()
    {
        $this->_calendarService = $this->_helper->getService('Core_Service_Calendar');
        $this->_memberService = $this->_helper->getService('Core_Service_Member');
        $this->_passiveService = $this->_helper->getService('Core_Service_Passive');
        $this->_shiftService = $this->_helper->getService('Core_Service_Shift');
    }

    public function indexAction()
    {
       // $this->_helper->acl()->isAllowed('action:admin.calendar', 'navigation');

    	$member_id = $this->view->authInfo()->getMemberId();
        $this->view->memberId = $member_id;

    	$addMessageForm = new Core_Form_Passive_Add();

        $newtimecode = null;
        $msg = '';

		$request = $this->getRequest();
		if ($request->isPost() && $addMessageForm->isValid($request->getPost())) {
            $postData = $addMessageForm->getValues();
			$postData['member_id'] = $member_id;

			$result = $this->_memberService->addPassiveMessage($postData);
			if ($result === Core_Service_Member::INVALID_PASSIVE_START_DATE) {
				$msg = $this->view->flashMessage()->renderMessage('Ugyldig startdato valgt', RedCross_View_Helper_FlashMessage::ERROR);
			}
			else if ($result === Core_Service_Member::INVALID_PASSIVE_END_DATE){
				$msg = $this->view->flashMessage()->renderMessage('Ugyldig sluttdato valgt', RedCross_View_Helper_FlashMessage::ERROR);
			}
			else if ($result === true) {
                $msg = $this->view->flashMessage()->renderMessage('Passivmelding lagt til', RedCross_View_Helper_FlashMessage::SUCCESS);

                $date = $request->getParam('from_date');
				$newtimecode = substr($date, 6, 4) . substr($date, 3, 2);
				$this->view->calendar = $this->_createCalendar($member_id, $newtimecode);
			}
			else {
				$msg = $this->view->flashMessage()->renderMessage('En feil oppstod under innleggelse', RedCross_View_Helper_FlashMessage::ERROR);
			}	
		}
        $this->view->flashMessage = $msg;
		
		if($newtimecode == null) {
			$timecode = $this->_getParam('date');
		} else {
			$timecode = $newtimecode;
		}
		$this->view->calendar = $this->_createCalendar($member_id, $timecode);
		
    	$addMessageForm->fillForm(array('member_id' => $member_id));
    	$this->view->addMessageForm = $addMessageForm;
    }
    public function createAction()
    {
	$form = new Admin_Form_Calendar_Add();
	$this->view->form = $form;

	$request = $this->getRequest();
	if($request->isPost() && $form->isValid($request->getPost())) {
		$data = $form->getValues();
		
		$calendar = new Core_Model_Calendar();
        
        $datetime = $data['date'] . ' ' . $data['time'];
        $date = new RedCross_Date($datetime, RedCross_Date::NO_DATE_FMT . ' ' . RedCross_Date::NO_TIME_FMT);
        $data['date'] = $date->export();

        $this-> calendarService->create($calendar); 

        return $this->_helper->redirector('view', 'calendar', 'core', array('id'=> $calendar->id));
    }
    }  
 
    private function _createCalendar($member_id, $timecode = null)
    {
		$date = new RedCross_Date();
    	if (!is_null($timecode) && !empty($timecode) && Zend_Date::isDate($timecode, self::PARAMETER_DATE_FORMAT)) {
            $year = substr($timecode, 0, 4);
            $month = substr($timecode, 4, 2);
            $date->setYear($year);
            $date->setMonth($month);
    	}
    	$date->setDay(1);
        $date->setHour(0);
        $date->setMinute(0);
        $date->setSecond(0);
        
    	$calendar = new Core_Model_Calendar($date);
    	
    	$anotherDate = new Redcross_Date($date);
    	$from_date = $anotherDate->export();
    	$anotherDate->add(1, Zend_Date::MONTH);
    	$to_date = $anotherDate->export();
    	
    	$passiveMessages = $this->_memberService->getPassiveMessages($member_id, $from_date, $to_date);
    	$calendar->setPassiveMessages($passiveMessages);
    	
    	$shifts = $this->_shiftService->fetchAllVisibleForMember($member_id, array('from_date' => $from_date, 'to_date' => $to_date));
    	$calendar->setShifts($shifts);
    	
		return $calendar;    	
    }
    
}
